package com.example.classcommunity3.service;

import com.example.classcommunity3.dao.SubjectDao;
import com.example.classcommunity3.model.Subject;
import com.example.classcommunity3.model.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

//业务逻辑
@Service
public class SubjectService {

    @Autowired
    SubjectDao subjectDao;

    @Autowired
    ClasstableService classtableService;

    public Subject getSubInfo(String subCode){
        /**
         @describe 获得课程的信息
         @param [subCode]
         @return com.example.classcommunity3.model.Subject
         @date 2018/6/9
        */
        return  subjectDao.findBySubCode(subCode);
    }

    public String getTName(Long tAccount){
        /**
         @describe 获得开课老师的名字
         @param [tAccount]
         @return java.lang.String
         @date 2018/6/9
        */
        TeacherService teacherService = new TeacherService();
        return teacherService.teacherDao.findByTAccount(tAccount).gettName();
    }

    public HashMap<String, Object> addSubject(String subname, Long taccount) {
        /**
         @describe 添加课程
         @param [subname, taccount]
         @return java.util.HashMap<java.lang.String,java.lang.Object>
         @date 2018/6/9
        */
        Subject subject = new Subject();
        HashMap<String, Object> hashMap = new HashMap<>();

        int i = (int) (Math.random() * 1000);
        String subCode = "" + i;

        while (subjectDao.findBySubCode(subCode) != null) {
            i = (int) (Math.random() * 1000);
            subCode = "" + i;
        }
        try {
            subject.setSubCode(subCode);
            subject.setSubName(subname);
            subject.setSubTAccount(taccount);
            subject.setSubStuNumber(0L);
            subjectDao.save(subject);

            hashMap.put("subCode", subCode);
            hashMap.put("verify", "true");
        } catch (Exception e) {
            hashMap.put("verify", "false");
        }
        return hashMap;
    }

    public Long updateStuNumber(String subCode) {
        /**
         @describe 更新课程人数
         @param [subCode]
         @return java.lang.Long
         @date 2018/6/9
        */
        Long stunumber = classtableService.getstunumber(subCode);
        Subject subject = subjectDao.findBySubCode(subCode);
        subject.setSubStuNumber(stunumber);
        subjectDao.save(subject);
        return stunumber;
    }

    public List<Subject> getAllTacherSub(Long account) {
        /**
         @describe 得到一个老师开的所有课程
         @param [account]
         @return java.util.List<com.example.classcommunity3.model.Subject>
         @date 2018/6/9
        */
        List<Subject> list = subjectDao.findBySubTAccount(account);

        return list;
    }

    //判断该课程是否存在
    public boolean existClass(String subCode){
        if (subjectDao.findBySubCode(subCode) != null){
            return true;
        }
        else
            return false;
    }

    // 删除课程
    public boolean deleteSub(String subCode){
        int i = subjectDao.deleteBySubCode(subCode);
        return true;
    }

}
